$(function(){
    Ext.onReady(function() {
        var obj = {
            grid: null,
            account: '',
            init: function () {
                var that = this;
                that.account = localStorage.getItem('account') || '';
                if(!that.account){
                    Ext.MessageBox.alert('温馨提示', '账户信息过期，请重新登录！');
                    window.location.replace('./login.html');
                    return;
                }
                var columns = [
                    {
                        "applicant": '13500000002',
                        "id" : "3",
                        "phone" : "15857502013",
                        "lxr" : "冯翔",
                        "fl" : "带薪年假、生日补贴、免费体检",
                        "address" : "华宇路108号",
                        "gw" : "门卫",
                        "zprs" : "10",
                        "xc" : "2000",
                        "xl": "高中",
                        "xs" : "绍兴县",
                        "xzjd" : "柯桥街道",
                        "zpyq" : "30-50，身体健康",
                        "zpdw" : "绿城物业",
                    }, {
                        "applicant": '13500000003',
                        "id" : "4",
                        "phone" : "15857502013",
                        "lxr" : "冯翔",
                        "fl" : "带薪年假、生日补贴、免费体检",
                        "address" : "昆仑大厦",
                        "gw" : "业务员",
                        "zprs" : "3",
                        "xc" : "600",
                        "xl": "高中",
                        "xs" : "越城区",
                        "xzjd" : "迪荡街道",
                        "zpyq" : "30-50，身体健康",
                        "zpdw" : "贝壳房产",
                    },
                    // {
                    //     "applicant": '13500000001',
                    //     "id" : "5",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "华宇路108号",
                    //     "gw" : "保安",
                    //     "zprs" : "10",
                    //     "xc" : "4000",
                    //     "学历": "初中",
                    //     "xs" : "绍兴县",
                    //     "xzjd" : "柯桥街道",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "绿城物业",
                    // }, {
                    //     "applicant": '13500034443',
                    //     "id" : "6",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "华宇路108号",
                    //     "gw" : "保安",
                    //     "zprs" : "10",
                    //     "xc" : "4000",
                    //     "学历": "小学",
                    //     "xs" : "绍兴县",
                    //     "xzjd" : "柯桥街道111",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "绿城物业",
                    // },{
                    //     "applicant": '13500000002',
                    //     "id" : "7",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "华宇路108号",
                    //     "gw" : "门卫",
                    //     "zprs" : "10",
                    //     "xc" : "2000",
                    //     "学历": "高中",
                    //     "xs" : "绍兴县",
                    //     "xzjd" : "柯桥街道",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "绿城物业",
                    // }, {
                    //     "applicant": '13500000003',
                    //     "id" : "8",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "昆仑大厦",
                    //     "gw" : "业务员",
                    //     "zprs" : "3",
                    //     "xc" : "600",
                    //     "xs" : "越城区",
                    //     "xzjd" : "迪荡街道",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "贝壳房产",
                    // },
                    // {
                    //     "applicant": '13500000001',
                    //     "id" : "9",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "华宇路108号",
                    //     "gw" : "保安",
                    //     "zprs" : "10",
                    //     "xc" : "4000",
                    //     "xs" : "绍兴县",
                    //     "xzjd" : "柯桥街道",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "绿城物业",
                    // }, {
                    //     "applicant": '13500034443',
                    //     "id" : "10",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "华宇路108号",
                    //     "gw" : "保安",
                    //     "zprs" : "10",
                    //     "xc" : "4000",
                    //     "xs" : "绍兴县",
                    //     "xzjd" : "柯桥街道111",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "绿城物业",
                    // },{
                    //     "applicant": '13500000002',
                    //     "id" : "11",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "华宇路108号",
                    //     "gw" : "门卫",
                    //     "zprs" : "10",
                    //     "xc" : "2000",
                    //     "xs" : "绍兴县",
                    //     "xzjd" : "柯桥街道",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "绿城物业",
                    // }, {
                    //     "applicant": '13500000003',
                    //     "id" : "12",
                    //     "phone" : "15857502013",
                    //     "lxr" : "冯翔",
                    //     "fl" : "带薪年假、生日补贴、免费体检",
                    //     "address" : "昆仑大厦",
                    //     "gw" : "业务员",
                    //     "zprs" : "3",
                    //     "xc" : "600",
                    //     "xs" : "越城区",
                    //     "xzjd" : "迪荡街道",
                    //     "zpyq" : "30-50，身体健康",
                    //     "zpdw" : "贝壳房产",
                    // }
                ];
                that.initExtList(columns)
            },
            initExtList: function (data) {
                var that = this;
                var  store = Ext.create("Ext.data.Store", {
                    fields: ['applicant','id', 'phone','lxr','fl','address','gw','zprs','xc','xs','xzjd','zpyq','zpdw'],
                    data:data
                });
                if(!data) {
                    // 接口请求
                    store = Ext.create("Ext.data.Store", {
                        fields: ['applicant','id', 'phone','lxr','fl','address','gw','zprs','xc','xl','xs','xzjd','zpyq','zpdw'],
                        autoLoad: true,
                        pageSize: 20, // 一页 20条
                        proxy: {
                            type: 'ajax',
                            url: $.getAjaxHost('def') + 'zpinfo/applicantList',
                            params: {
                                account: localStorage.getItem('account')
                            },
                            // page 第几页 1
                            // start 开始页 0开始
                            // limit 页面大小
                            reader: {
                                type: 'json',
                                root: 'list',
                                totalProperty: 'total' // 总数对应字段名称
                            }
                        }
                    });
                }
                this.grid = new Ext.grid.GridPanel({
                    renderTo: document.body,
                    title: '岗位信息列表',
                    width: '100%',
                    height: '100vh', // 2020205
                    viewConfig:{
                        forcefit:true,
                        stripeRows:true
                    },
                    autoHeight : true,
                    stripeRows: true,
                    loadMask:true,
                    autoExpandColumn: 'id',
                    stateful: true,
                    stateId: 'grid',
                    layout: 'fit',
                    fullscreen: true,
                    selModel: { singleSelection:true,selType: 'checkboxmodel'},
                    columns: [
                        { text: '编号', dataIndex: 'id', width: 50,sortable : true },
                        { text: '公司名称', dataIndex: 'zpdw', width: 120,sortable : true },
                        { text: '岗位', dataIndex: 'gw', width: 150,sortable : true },
                        { text: '区域', dataIndex: 'xs', width: 80,sortable : true },
                        { text: '街道', dataIndex: 'xzjd', width: 120,sortable : true },
                        { text: '地址', dataIndex: 'address', width: 120,sortable : true },
                        { text: '岗位待遇（元/月）', dataIndex: 'xc', width: 150,sortable : true },
                        { text: '招聘人数', dataIndex: 'zprs', width: 150,sortable : true },
                        { text: '招聘要求', dataIndex: 'zpyq', width: 250,sortable : false},
                        { text: '学历要求', dataIndex: 'xl', width: 250,sortable : false},
                        { text: '福利', dataIndex: 'fl', width: 250,sortable : false},
                        { text: '联系人姓名', dataIndex: 'lxr', width: 150,sortable : false},
                        { text: '联系人电话', dataIndex: 'phone', width: 120,sortable : false}
                    ],
                    listeners: {
                        itemclick: function (view, record, item, index, e) {
                            //record.data;
                        }
                    },
                    store: store,
                    tbar:[
                        {text:'新增',iconCls:'x-fa x-grid-group-title',handler:function(){
                            that.handleCreateNewWin(that)
                        }},"-",
                        {text:'编辑',iconCls:'x-fa fa-cog',handler:function(){
                            that.handleCreateEditWin(that)
                        }},"-",
                        {text:'删除',iconCls:'x-fa fa-delete',
                            handler:function () {
                                var selModel = that.grid.getSelectionModel();
                                if  (selModel.hasSelection()) {
                                    Ext.Msg.confirm( "警告" ,  "确定要删除吗？" ,  function (button) {
                                        if (button == "yes") {
                                            var  selections  =  selModel.selected.items;
                                            Ext.Ajax.request({
                                                url: $.getAjaxHost('def') + 'zpinfo/deleteApplicantInfo',
                                                params: {items: selections},
                                                success: function (response, selfobj) {
                                                    Ext.each(selections,  function (item)  {
                                                        store.remove(item);
                                                    } );
                                                    that.grid.store.reload(); // 重新加载数据
                                                },
                                                failure: function () {
                                                    Ext.Msg.alert('提示', '删除错误,请重试');
                                                }
                                            });
                                        }
                                    });
                                }
                            }
                        },"-"
                    ],
                    bbar : {
                        xtype : 'pagingtoolbar',
                        pageSize: 20,
                        store: store,
                        displayInfo: true,
                        displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
                        emptyMsg: "没有数据"
                    }

                });
            },
            handleCreateNewWin: function (e) {
                var win;
                var that = e;
                var xlList = Ext.create("Ext.data.Store",{
                    fields: ['name', 'id'],
                    data:[
                        {name:'初中',id:'初中'},
                        {name:'高中',id:'高中'},
                        {name:'大专',id:'大专'},
                        {name:'本科',id:'本科'},
                        {name:'研究生及以上',id:'研究生及以上'}
                    ]
                });
                var ztList = Ext.create("Ext.data.Store",{
                    fields: ['name', 'id'],
                    data:[
                        {name:'上线',id:'上线'},
                        {name:'下线',id:'下线'}
                    ]
                });
                var formPanel = new Ext.form.FormPanel({
                    xtype: 'form',
                    width: '100%',
                    border: 0,
                    layoutConfig:{columns:2},
                    defaultType: 'textfield',
                    bodyPadding: 10,
                    items: [
                        {
                            id: 'gw',
                            fieldLabel: '岗位',
                            name: 'gw',
                            width: '100%',
                            allowBlank: false,
                            tooltip: '请输入岗位名称',
                            blankText: '请输入岗位名称',
                        },
                        {
                            id: 'xc',
                            xtype: 'numberfield',
                            fieldLabel: '岗位待遇(元/月)',
                            name: 'xc',
                            width: '50%',
                            hideTrigger: true,
                            allowBlank: false,
                            tooltip: '请输入岗位待遇',
                            blankText: '请输入岗位待遇',
                        },
                        {
                            id: 'zprs',
                            xtype: 'numberfield',
                            fieldLabel: '招聘人数',
                            name: 'zprs',
                            width: '45%',
                            style:{
                                'position': 'absolute',
                                'left': '53%',
                                'margin-top': '-5px',
                                'transform': 'translate(0,-100%);'
                            },
                            allowBlank: false,
                            allowDecimals: false,
                            minValue: 1,
                            tooltip: '请输入招聘人数',
                            blankText: '请输入招聘人数',
                        },
                        {
                            id: 'xl',
                            fieldLabel: '学历要求',
                            name: 'industry',
                            width: '100%',
                            xtype:'combobox',
                            listConfig:{
                                emptyText:'没有找到匹配的数值',
                                maxHeight:200
                            },
                            queryMode:'local',//[local|remot]
                            store:xlList,
                            valueField:"id",
                            displayField :'name',
                            forceSelection:true,//不允许输入数据集合中没有的数值
                            typeAhead : true,//自动补全，默认为false
                            value:'',
                            allowBlank: false,
                            tooltip: '请选择学历要求',
                            blankText: '请选择学历要求',
                            prompt: '请选择学历要求'
                        },
                        {
                            id: 'zpyq',
                            xtype: "textarea",
                            fieldLabel: '招聘要求',
                            name: 'zpyq',
                            width: '100%',
                            allowBlank: false,
                            tooltip: '请输入招聘要求',
                            blankText: '请输入招聘要求',
                        },
                        {
                            id: 'fl',
                            xtype: "textarea",
                            fieldLabel: '福利',
                            name: 'fl',
                            width: '100%',
                            allowBlank: false,
                            tooltip: '请输入福利',
                            blankText: '请输入福利',
                        },
                        {
                            id: 'zt',
                            fieldLabel: '状态',
                            name: 'industry',
                            width: '50%',
                            xtype:'combobox',
                            listConfig:{
                                emptyText:'没有找到匹配的数值',
                                maxHeight:200
                            },
                            queryMode:'local',//[local|remot]
                            store:ztList,
                            valueField:"id",
                            displayField :'name',
                            forceSelection:true,//不允许输入数据集合中没有的数值
                            typeAhead : true,//自动补全，默认为false
                            value:'上线',
                            allowBlank: false,
                            tooltip: '请选择状态',
                            blankText: '请选择状态',
                            prompt: '请选择状态'
                        },
                    ]
                })
                win=new Ext.Window({//ext窗口，html把显示的值对应好就行。
                    title:"岗位信息新增",
                    width:500,
                    maximizable:true,
                    items:formPanel,
                    buttons: [
                        {
                            text: '新增',
                            handler: function () {
                                var form = formPanel.getForm(); // get the form panel
                                if (form.isValid()) {
                                    form.doAction('submit', {
                                        url: '11',//$.getAjaxHost('def') + 'zpinfo/addJobInfo',
                                        method: 'POST',
                                        data: {
                                            account: localStorage.getItem('account')
                                        },
                                        waitTitle: '连接中',
                                        waitMsg: '传送数据...',
                                        success: function () {
                                            Ext.Msg.alert('提示',
                                                '新增成功!'
                                            );
                                            win.close();
                                            that.grid.store.reload();
                                        },
                                        failure: function (form, action) {
                                            Ext.Msg.alert('错误提示', '非常抱歉!,新增失败');
                                            //form.reset()
                                        }
                                    });

                                } else { // display error alert if the data is invalid
                                    Ext.Msg.alert('错误提示', '请完善发布信息！');
                                }
                            }
                        }
                    ]
                });
                win.show();//渲染显示
            },
            handleCreateEditWin: function (e) {

            }
        };
        obj.init();
    });
});